package com.eva.api.system;

import com.eva.api.BaseController;
import com.eva.core.annotation.pr.PreventRepeat;
import com.eva.core.constants.Constants;
import com.eva.core.model.ApiResponse;
import com.eva.core.model.PageWrap;
import com.eva.core.model.PageData;
import com.eva.dao.system.model.SystemInterfaceConfig;
import com.eva.dao.system.dto.QuerySystemInterfaceConfigListDTO;
import com.eva.dao.system.vo.SystemInterfaceConfigListVO;
import com.eva.service.system.SystemInterfaceConfigService;
import com.eva.service.system.SystemInterfaceConfigListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统接口配置接口
 * @author Eva.Caesar Liu
 * @date 2022/04/02 21:57
 */
@RestController
@RequestMapping("/system/interface/config")
@Api(tags = "系统接口配置接口")
public class SystemInterfaceConfigController extends BaseController {

    @Autowired
    private SystemInterfaceConfigListService systemInterfaceConfigListService;
    
    @Autowired
    private SystemInterfaceConfigService systemInterfaceConfigService;

    @PreventRepeat
    @ApiOperation("新建")
    @PostMapping("/create")
    @RequiresPermissions("system:interface:config:create")
    public ApiResponse create(@Validated(Constants.OperaType.Create.class) @RequestBody SystemInterfaceConfig systemInterfaceConfig) {
        return ApiResponse.success(systemInterfaceConfigService.create(systemInterfaceConfig));
    }

    @ApiOperation("根据ID删除")
    @GetMapping("/delete/{id}")
    @RequiresPermissions("system:interface:config:delete")
    public ApiResponse deleteById(@PathVariable Integer id) {
        systemInterfaceConfigService.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @GetMapping("/delete/batch")
    @RequiresPermissions("system:interface:config:delete")
    public ApiResponse deleteByIds(@RequestParam String ids) {
        String [] idArray = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Integer.valueOf(id));
        }
        systemInterfaceConfigService.deleteByIdInBatch(idList);
        return ApiResponse.success(null);
    }

    @ApiOperation("根据ID修改")
    @PostMapping("/updateById")
    @RequiresPermissions("system:interface:config:update")
    public ApiResponse updateById(@Validated(Constants.OperaType.Update.class) @RequestBody SystemInterfaceConfig systemInterfaceConfig) {
        systemInterfaceConfigService.updateById(systemInterfaceConfig);
        return ApiResponse.success(null);
    }

    @ApiOperation("修改配置状态")
    @PostMapping("/updateStatus")
    @RequiresPermissions("system:interface:config:update")
    public ApiResponse updateStatus(@Validated(Constants.OperaType.UpdateStatus.class) @RequestBody SystemInterfaceConfig systemInterfaceConfig) {
        systemInterfaceConfigService.updateById(systemInterfaceConfig);
        return ApiResponse.success(null);
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @RequiresPermissions("system:interface:config:query")
    public ApiResponse<PageData<SystemInterfaceConfigListVO>> findPage (@RequestBody PageWrap<QuerySystemInterfaceConfigListDTO> pageWrap) {
        return ApiResponse.success(systemInterfaceConfigListService.findPage(pageWrap));
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    @RequiresPermissions("system:interface:config:query")
    public ApiResponse findById(@PathVariable Integer id) {
        return ApiResponse.success(systemInterfaceConfigService.findById(id));
    }
}
